package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.OrderSettingService;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{
    @Autowired
    private OrderSettingDao orderSettingDao;
    //批量导入预约设置数据
    public void add(List<OrderSetting> list) {
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                //判断当前日期是否已经进行了预约设置
                Long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(countByOrderDate!=null&&countByOrderDate> 0){
                    //有-->执行更新语句
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //没有-->插入语句
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }
    //根据月份查询对应的预约设置数据
    @Override
    public List<Map> findByMonth(String date) {
        //拼-例:2019-6-1
        String begin=date+"-1";
        String end=date+"-31";
        Map<String,String>map=new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);

        List<OrderSetting>list=orderSettingDao.findByMonth(map);
        List<Map>result=new ArrayList<>();
        if (list!=null&&list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object>m=new HashMap<>();
                //日期
                m.put("date", orderSetting.getOrderDate().getDate());//对应页面数据格式
                //预约容量
                m.put("number", orderSetting.getNumber());
                //已预约人数
                m.put("reservations", orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断日期是否进行预约设置
        Date orderDate = orderSetting.getOrderDate();
        Long countByOrderDate = orderSettingDao.findCountByOrderDate(orderDate);
        //日期已经进行预约设置
        if (countByOrderDate>0){
            //更新操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //日期已经没有进行预约设置
            //插入操作
            orderSettingDao.add(orderSetting);
        }

    }
}


